package me.cjj.android.assignment.model

class QuadraticQuestion(val a: Int, val b: Int, val c: Int) : IQuestion {

    override fun getAnswerCount(): Int {
        return 2
    }

    override fun getQuestionText(): String {
        val aText = a.toQuestionText(true, "x^2")
        val bText = b.toQuestionText(x = "x")
        val cText = c.toQuestionText()
        return "\\($aText$bText$cText=0\\)"
    }

    fun isValid(): Boolean {
        return -4 * a * c + b * b > 0
    }

    override fun judge(vararg answers: Float): Boolean {
        val (correct1: Float, correct2: Float) = getAnswer()
        if ((answers[0] == correct1 && answers[1] == correct2) || (answers[1] == correct1 && answers[0] == correct2)) {
            return true
        }
        return false
    }

    private fun getAnswer(): Pair<Float, Float> {
        val sqrt = Math.sqrt((b * b - 4 * a * c).toDouble())
        var correct1: Float = (sqrt - b).toFloat() / (2 * a).toFloat()
        var correct2: Float = (-sqrt - b).toFloat() / (2 * a).toFloat()
        correct1 = (Math.round(correct1 * 100.0) / 100.0).toFloat()
        correct2 = (Math.round(correct2 * 100.0) / 100.0).toFloat()
        return Pair(correct1, correct2)
    }

    override fun getAnswerText(): String {
        val (correct1: Float, correct2: Float) = getAnswer()
        return "$correct1 , $correct2"
    }

}